package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.App;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.custom.Attributes;
import com.luck.easyfoodbatch.custom.SwipeAdapterInterface;
import com.luck.easyfoodbatch.custom.SwipeItemMangerImpl;
import com.luck.easyfoodbatch.custom.SwipeItemMangerInterface;
import com.luck.easyfoodbatch.custom.SwipeLayout;
import com.luck.easyfoodbatch.mvp.model.OrderDetailM;
import com.luck.easyfoodbatch.ui.ReturnGoodsActivity;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.T;
import com.luck.easyfoodbatch.util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.adapter
 * email：893855882@qq.com
 * data：16/9/29
 */
public class OrderDetailsShopAdapter extends RecyclerView.Adapter<OrderDetailsShopAdapter.ItemHolder> {
    private Context mContext;
    private List<OrderDetailM.DataBean.OrderInfoBean> list = new ArrayList<>();
    private String shopName;
    private int status;
    private OrderDetailM.DataBean orderDetailM;

    public OnReturnGoods onReturnGoods;

    public void setOrderDetailM(OrderDetailM.DataBean orderDetailM) {
        this.orderDetailM = orderDetailM;
    }

    public interface OnReturnGoods {
        void onReturnShop(OrderDetailM.DataBean.OrderInfoBean infoBean, OrderDetailM.DataBean orderDetailM);
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    public OrderDetailsShopAdapter(Context mContext, OnReturnGoods onReturnGoods) {
        super();
        this.mContext = mContext;
        this.onReturnGoods = onReturnGoods;
    }

    public void setList(List<OrderDetailM.DataBean.OrderInfoBean> list) {
        this.list = list;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    @Override
    public OrderDetailsShopAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.orderdetails_shop_item, parent, false);
        return new ItemHolder(rootView);
    }


    @Override
    public void onBindViewHolder(final OrderDetailsShopAdapter.ItemHolder holder, final int position) {
        final OrderDetailM.DataBean.OrderInfoBean infoBean = list.get(position);
        if (position == 0) {
            holder.tv_shop_name.setText(shopName);
            holder.tv_shop_name.setVisibility(View.VISIBLE);
            holder.tv_shop_line.setVisibility(View.VISIBLE);
        } else {
            holder.tv_shop_name.setVisibility(View.GONE);
            holder.tv_shop_line.setVisibility(View.GONE);
        }
        if (status == 0) {
            holder.tv_customer.setVisibility(View.GONE);
        } else {
            holder.tv_customer.setVisibility(View.VISIBLE);
        }
        String goods_thumb = infoBean.getGoods_thumb();
        String goods_name = infoBean.getGoods_name();
        String propertyTags = infoBean.getPropertyTags();
        int due_date = infoBean.getDue_date();
        int num = infoBean.getNum();
        String price = infoBean.getPrice();
        String real_price = infoBean.getReal_price();
        if (!Tools.isNull(goods_thumb)) {
            String path = "";
            if (goods_thumb.startsWith("http")) {
                path = goods_thumb;
            } else {
                path = HttpUtil.IMG_THUMB_BASE_URL + goods_thumb;
            }
            Glide.with(mContext)
                    .load(path)
                    .centerCrop()
                    .placeholder(R.mipmap.loading_4b3)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.iv_banner);
        } else {
            holder.iv_banner.setImageResource(R.mipmap.loading_4b3);
        }
        holder.tv_title.setText(goods_name);
        holder.tv_spec.setText(propertyTags);
        holder.tv_due_date.setText("保质期 至 " + Tools.SubTime2(due_date + ""));
        holder.tv_num.setText("x " + num);
        holder.tv_money.setText("￥" + real_price);

        holder.tv_customer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onReturnGoods.onReturnShop(infoBean, orderDetailM);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public class ItemHolder extends RecyclerView.ViewHolder {
        TextView tv_shop_name;
        TextView tv_shop_line;
        TextView tv_title;
        TextView tv_spec, tv_due_date, tv_num, tv_money;
        ImageView iv_banner;
        TextView tv_customer;

        public ItemHolder(View view) {
            super(view);
            tv_shop_name = (TextView) view.findViewById(R.id.tv_shop_name);
            tv_shop_line = (TextView) view.findViewById(R.id.tv_shop_line);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
            tv_spec = (TextView) view.findViewById(R.id.tv_spec);
            tv_due_date = (TextView) view.findViewById(R.id.tv_due_date);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            iv_banner = (ImageView) view.findViewById(R.id.iv_banner);
            tv_money = (TextView) view.findViewById(R.id.tv_money);
            tv_customer = (TextView) view.findViewById(R.id.tv_customer);
        }
    }

}
